Skip to content

refactor: optimize Application Insights telemetry and prevent duplicate logging handlers#367

Open
Harmanpreet-Microsoft wants to merge 11 commits intodevfrom
psl-abhar
Open

refactor: optimize Application Insights telemetry and prevent duplicate logging handlers#367
Harmanpreet-Microsoft wants to merge 11 commits intodevfrom
psl-abhar

Conversation

@Harmanpreet-Microsoft
Copy link
Contributor

@Harmanpreet-Microsoft Harmanpreet-Microsoft commented Mar 16, 2026

This pull request improves the reliability and correctness of telemetry and logging integration in the backend, focusing on Application Insights and OpenTelemetry. It ensures proper deduplication of logging handlers, simplifies telemetry client initialization, and updates tests for better coverage and realistic configuration.

Telemetry and logging improvements:

  • Changed Application Insights telemetry client initialization in event_utils.py to use the default (buffered/async) channel configuration, removing custom synchronous channel setup. This simplifies the code and aligns with best practices.
  • Enhanced OpenTelemetry logging handler setup in app.py to remove stale handlers before adding a new one, preventing duplicate LoggingHandler instances and potential resource leaks. The handler is safely closed if removed.

Testing enhancements:

  • Updated test cases in event_utils_test.py to use a valid UUID format for the Application Insights instrumentation key, making tests more realistic and robust. [1] [2] [3] [4] [5]
  • Added a new test in app_test.py to verify that repeated calls to create_app() do not accumulate multiple LoggingHandler instances, ensuring proper deduplication and cleanup.
  • Minor test imports and setup improvements in app_test.py for logging and OpenTelemetry.

Other minor changes:

  • Cleaned up unused imports in event_utils.py related to Application Insights channel configuration.
  • Clarified event flushing logic in track_event_if_configured for immediate sending of telemetry events.

Does this introduce a breaking change?

  • Yes
  • No

Golden Path Validation

  • I have tested the primary workflows (the "golden path") to ensure they function correctly without errors.

Deployment Validation

  • I have validated the deployment process successfully and all services are running as expected with this change.

What to Check

Verify that the following are valid

  • ...

Other Information

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the backend’s Application Insights / OpenTelemetry telemetry setup to simplify client initialization and avoid duplicate log exporting during repeated app creation, and updates event-tracking tests to match the new behavior.

Changes:

  • Simplifies Application Insights TelemetryClient initialization by removing explicit synchronous channel setup.
  • Adds a guard to prevent adding duplicate OpenTelemetry LoggingHandler instances to the root logger.
  • Updates track_event_if_configured tests by removing flush() assertions.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
src/backend/api/event_utils.py Simplifies TelemetryClient initialization and tweaks flush() call/comment formatting.
src/backend/app.py Prevents duplicate OpenTelemetry logging handlers on the root logger.
src/tests/backend/api/event_utils_test.py Removes flush() assertions from telemetry event tracking tests.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors backend telemetry wiring to simplify Application Insights client initialization and reduce duplicated OpenTelemetry log handlers, aiming to improve runtime behavior during reload/testing and streamline telemetry configuration.

Changes:

  • Removed explicit synchronous Application Insights channel setup and rely on TelemetryClient defaults.
  • Added root logger LoggingHandler de-duplication logic to avoid accumulating duplicate OTel handlers.
  • Adjusted comments around event flushing behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/backend/app.py Removes existing OTel LoggingHandler instances from the root logger before adding a fresh one tied to the current logger_provider.
src/backend/api/event_utils.py Simplifies TelemetryClient initialization by removing the explicit synchronous channel and keeps explicit flush() after event tracking.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors backend telemetry and logging initialization to simplify Application Insights client setup and avoid duplicated OpenTelemetry log handlers when the app is created multiple times (e.g., tests/hot-reload).

Changes:

  • Simplified Application Insights TelemetryClient initialization by removing explicit synchronous channel wiring.
  • Updated create_app() logging setup to remove existing OpenTelemetry LoggingHandler instances before adding a new one.
  • Minor refactor/comment tweak around event flushing; test module imports were updated (but currently appear unused).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/backend/app.py Deduplicates OpenTelemetry LoggingHandler on the root logger during app startup.
src/backend/api/event_utils.py Uses default TelemetryClient channel config instead of explicit synchronous channel types; keeps explicit flush.
src/tests/backend/app_test.py Adds imports intended for telemetry/logging-related testing, but no assertions yet.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

… variable assignments and enhance error logging during cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants